package com.xxxx.server;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.junit.Test;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @program: yeb
 * @description: 测试
 * @author: Honors
 * @create: 2021-08-28 16:06
 */
public class test1 {

    @Test
    public void test2() throws IOException, InvalidFormatException {
        File outFile = new File("D:\\4.docx");

//        PoiWorldUtils poiWaorldUtils = new PoiWorldUtils();

        FileOutputStream out = new FileOutputStream(outFile);
        XWPFDocument docx = new XWPFDocument();
        XWPFTable table = docx.createTable(1, 6);

        FileInputStream png = new FileInputStream(new File("F:\\桌面文档\\汇思\\zhjx.png"));

        // 表格第一行第一列插入图片
        XWPFParagraph xwpfParagraph = table.getRow(0).getCell(0).addParagraph();

        XWPFRun xwpfPicture = xwpfParagraph.createRun();

        xwpfPicture.addPicture(png, XWPFDocument.PICTURE_TYPE_PNG, "1.png",
                Units.toEMU(40), Units.toEMU(40));
        xwpfPicture.addBreak();

        // 设置表格第一行第一列
        table.getRow(0).getCell(0).setText("培训成绩表");
        // 第一行单元格设置
        XWPFTableCell cell = table.getRow(0).getCell(0);
        // 单元格文本居中
        cellTextCenter(cell);

        png.close();
        docx.write(out);
        out.close();

    }

    /**
     * 设置单元格文本水平居中
     *
     * @param cell
     */
    public static void cellTextCenter(XWPFTableCell cell) {
        CTTc cttc = cell.getCTTc();
        CTTcPr ctPr = cttc.addNewTcPr();
        ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
        cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
    }

    @Test
    public void test() {
        // 准备数据
        List<String> list = new ArrayList();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("c");
        list.add("b");
        list.add("a");
        // 利用list中的元素创建HashSet集合，此时set中进行了去重操作
        HashSet set = new HashSet(list);
        // 清空list集合
        list.clear();
        // 将去重后的元素重新添加到list中
        list.addAll(set);

        for (String s : list) {
            System.out.println(s);
        }

    }
}
